﻿using Dal.Iterfease;
using Model;
using Model.viewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dal.infDal
{
    public class SiteDal : IsiteDal
    {
        private readonly AppDbContext _appDbContext;

        public SiteDal(AppDbContext appDbContext)
        {
            _appDbContext = appDbContext;
        }

        /// <summary>
        /// 添加地址联动
        /// </summary>
        /// <param name="Ssid"></param>
        /// <returns></returns>
        public List<Ssx> getSsx(int Ssid)
        {
            return _appDbContext.Ssx.Where(u => u.Ssid == Ssid).ToList();
        }

        /// <summary>
        /// 显示地址
        /// </summary>
        /// <param name="Suid"></param>
        /// <returns></returns>
        public List<SiteModel> getSite(int Suid)
        {
            var sql = from a in _appDbContext.siteInfo
                      join b in _appDbContext.Ssx on a.shengid equals b.Xid
                      join c in _appDbContext.Ssx on a.shiid equals c.Xid
                      join d in _appDbContext.Ssx on a.xianid equals d.Xid
                      orderby a.Sdefault
                      where a.Suid == Suid
                      select new SiteModel
                      {
                          Sid = a.Sid,
                          Sname = a.Sname,
                          Sadds = a.Sadds,
                          Sphone = a.Sphone,
                          Sdefault = a.Sdefault,
                          shengname = b.Xname,
                          shiname = c.Xname,
                          xianname = d.Xname,
                          shengid = b.Xid,
                          shiid = c.Xid,
                          xianid = d.Xid,
                      };
            return sql.ToList();
        }

        /// <summary>
        /// 地址添加
        /// </summary>
        /// <param name="sit"></param>
        /// <returns></returns>
        public int AddSite(siteInfo sit)
        {
            _appDbContext.siteInfo.Add(sit);
            return _appDbContext.SaveChanges();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Sid"></param>
        /// <returns></returns>
        public int DelSite(int Sid)
        {
            var a = _appDbContext.siteInfo.FirstOrDefault(u => u.Sid == Sid);
            _appDbContext.Remove(a);
            return _appDbContext.SaveChanges();
        }

        /// <summary>
        /// 地址修改
        /// </summary>
        /// <param name="sit"></param>
        /// <returns></returns>
        public int UpStie(siteInfo sit)
        {
            _appDbContext.Entry(sit).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return _appDbContext.SaveChanges();
        }


    }
}
